//
//  TEDatabaseObjectViewController.m
//  TintExample
//
//  Created by Kevin Wu on 9/4/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "TEDatabaseObjectViewController.h"

#define SPECIAL_ENTRY_ONE 80001
#define SPECIAL_ENTRY_TWO 80002
#define SPECIAL_ENTRY_THREE 80003

#define GROUP_ENTRY_BEGIN 90001
#define GROUP_ENTRY_END 90010


@implementation TEDatabaseObjectViewController


#pragma mark - Memory

- (id)init
{
    self = [super init];
    if ( self ) {
    }
    return self;
}

- (void)dealloc
{
    TTRelease(_db);
    _test = nil;
    [super dealloc];
}


#pragma mark - UIViewController

- (void)loadView
{
    [super loadView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addButtonWithTitle:@"Create database" selector:@selector(createDatabase:) type:1];
    [self addButtonWithTitle:@"Create table" selector:@selector(createTable:) type:1];
    
    [self addButtonWithTitle:@"Query" selector:@selector(query:) type:2];
    [self addButtonWithTitle:@"Insert" selector:@selector(insert:) type:2];
    [self addButtonWithTitle:@"Update" selector:@selector(update:) type:2];
    
    [self addButtonWithTitle:@"Destroy" selector:@selector(destroy:) type:3];
    [self addButtonWithTitle:@"Is exist" selector:@selector(isExist:) type:3];
    [self addButtonWithTitle:@"Reload" selector:@selector(reload:) type:3];
    
    [self addButtonWithTitle:@"Count" selector:@selector(countObjects:) type:4];
    [self addButtonWithTitle:@"Delete" selector:@selector(deleteObjects:) type:4];
    [self addButtonWithTitle:@"Update" selector:@selector(updateObjects:) type:4];
    [self addButtonWithTitle:@"Select" selector:@selector(selectObjects:) type:4];
}


#pragma mark - Public

- (void)createDatabase:(id)sender
{
    if ( _db == nil ) {
        NSString *path = [TTDocumentsPath() stringByAppendingPathComponent:@"databaseobject.sqlite"];
        _db = [[TTDatabase alloc] initWithPath:path];
        [_db open];
        [TTDatabaseObject setCurrentDatabase:_db];
    }
}

- (void)createTable:(id)sender
{
    [TEDBObject createTable];
}


- (void)query:(id)sender
{
    NSArray *result = nil;
    
    // Select special
    NSLog(@"special=============================================================");
    result = [TEDBObject selectObjectsOrderBy:@"f DESC" 
                                        where:@"a>? AND a<?", [NSNumber numberWithInt:SPECIAL_ENTRY_ONE-1], [NSNumber numberWithInt:SPECIAL_ENTRY_THREE+1], nil];
    for ( int i=0; i<[result count]; ++i ) {
        TEDBObject *object = [result objectAtIndex:i];
        NSLog(@"%@", object);
    }
    
    
    // Select group
    NSLog(@"group (date asc) ==================================================");
    result = [TEDBObject selectObjectsOrderBy:@"c DESC, f ASC" 
                                        where:@"a>? AND a<?", [NSNumber numberWithInt:GROUP_ENTRY_BEGIN-1], [NSNumber numberWithInt:GROUP_ENTRY_END+1], nil];
    for ( int i=0; i<[result count]; ++i ) {
        TEDBObject *object = [result objectAtIndex:i];
        NSLog(@"%@", object);
    }
    
    
    NSLog(@"group (date desc) ===================================================");
    result = [TEDBObject selectObjectsOrderBy:@"c ASC, f DESC" 
                                        where:@"a>? AND a<?", [NSNumber numberWithInt:GROUP_ENTRY_BEGIN-1], [NSNumber numberWithInt:GROUP_ENTRY_END+1], nil];
    for ( int i=0; i<[result count]; ++i ) {
        TEDBObject *object = [result objectAtIndex:i];
        NSLog(@"%@", object);
    }
    
}

- (void)insert:(id)sender
{
    
    TEDBObject *object = nil;
    
//    int a;
//    BOOL b;
//    long long c;
//    double d;
//    NSString *e;
//    NSDate *f;
    
    // empty
    object = [[TEDBObject alloc] init];
    [object save];
    TTRelease(object);
    
    object = [[TEDBObject alloc] init];
    [object save];
    TTRelease(object);
    
    
    
    NSDate *now = [NSDate date];
    
    // special
    object = [[TEDBObject alloc] init];
    object.a = SPECIAL_ENTRY_ONE;
    object.b = NO;
    object.c = SPECIAL_ENTRY_THREE;
    object.d = 1.1;
    object.e = @"AA";
    object.f = now;
    [object save];
    TTRelease(object);
    
    object = [[TEDBObject alloc] init];
    object.a = SPECIAL_ENTRY_TWO;
    object.b = YES;
    object.c = SPECIAL_ENTRY_TWO;
    object.d = 2.2;
    object.e = @"BB";
    object.f = [now dateByAddingDays:1];
    [object save];
    TTRelease(object);
    
    object = [[TEDBObject alloc] init];
    object.a = SPECIAL_ENTRY_THREE;
    object.b = NO;
    object.c = SPECIAL_ENTRY_ONE;
    object.d = 3.3;
    object.e = @"CC";
    object.f = [now dateByAddingDays:2];
    [object save];
    TTRelease(object);
    
    
    // group
    for ( int i=GROUP_ENTRY_BEGIN; i<=GROUP_ENTRY_END; ++i) {
        object = [[TEDBObject alloc] init];
        object.a = i;
        object.b = ( i%2 == 0 ); // NO, YES
        object.c = ( i - GROUP_ENTRY_BEGIN ) / 3 + 1;
        object.d = 100.123456789 - (i - GROUP_ENTRY_BEGIN);
        object.e = [NSString stringWithFormat:@"%c", 'A' + i-GROUP_ENTRY_BEGIN];
        object.f = [now dateByAddingDays:i-GROUP_ENTRY_BEGIN];
        [object save];
        TTRelease(object);
    }
    
}

- (void)update:(id)sender
{
    TEDBObject *object = nil;
    
    object = [TEDBObject firstObjectWhere:@"a=?", [NSNumber numberWithInt:SPECIAL_ENTRY_ONE], nil];
    object.e = @"TT";
    [object save];
    
    object = [TEDBObject firstObjectWhere:@"a=?", [NSNumber numberWithInt:SPECIAL_ENTRY_TWO], nil];
    object.e = @"GG";
    object.c = 110;
    [object save:@"e"];
    
    object = [TEDBObject firstObjectWhere:@"a=?", [NSNumber numberWithInt:SPECIAL_ENTRY_THREE], nil];
    object.e = @"GG";
    object.c = 110;
    [object save:@"c"];
    
}


- (void)destroy:(id)sender
{
    TEDBObject *object = nil;
    
    object = [[TEDBObject alloc] init];
    object.pk = SPECIAL_ENTRY_TWO;
    [object destroy];
    TTRelease(object);
    
}

- (void)isExist:(id)sender
{
    BOOL exist1 = NO;
    BOOL exist2 = NO;
    BOOL exist3 = NO;
    
    
    TEDBObject *object = nil;
    
    object = [[TEDBObject alloc] init];
    object.pk = SPECIAL_ENTRY_ONE;
    exist1 = [object isExist];
    TTRelease(object);
    
    object = [[TEDBObject alloc] init];
    object.pk = SPECIAL_ENTRY_TWO;
    exist2 = [object isExist];
    TTRelease(object);
    
    object = [[TEDBObject alloc] init];
    object.pk = SPECIAL_ENTRY_THREE;
    exist3 = [object isExist];
    TTRelease(object);
    
    NSLog(@"%d %d %d", exist1, exist2, exist3);
}

- (void)reload:(id)sender
{
}


- (void)countObjects:(id)sender
{
    NSLog(@"%d", [TEDBObject count]);
}

- (void)deleteObjects:(id)sender
{
}

- (void)updateObjects:(id)sender
{
}

- (void)selectObjects:(id)sender
{
}


@end
